package com.example.retrofitmvcdemo.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.retrofitmvcdemo.R;
import com.example.retrofitmvcdemo.beans.UserBean;
import com.example.retrofitmvcdemo.listener.RetrofitListener;
import com.example.retrofitmvcdemo.model.UserModel;
import com.example.retrofitmvcdemo.service.UserService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity implements RetrofitListener {
    private EditText etuser,etpass;
    private Button btnlogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        etuser = findViewById(R.id.etuser);
        etpass = findViewById(R.id.etpass);
        btnlogin = findViewById(R.id.button);

        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });
    }

    void login() {
        UserModel userModel = new UserModel();
        userModel.userLogin(etuser.getText().toString().trim(), etpass.getText().toString().trim(),this);
    }

    public void onSuccess(Object o,int flag){
        if(o!=null&&((UserBean)o).getUserid().equals(String.valueOf(1)))
            Toast.makeText(MainActivity.this, "登陆成功 "+((UserBean)o).getUserid(), Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(MainActivity.this, "登陆失败 "+((UserBean)o).getUserid(), Toast.LENGTH_SHORT).show();

    }
    public void onFail(){
        Toast.makeText(MainActivity.this, "网络错误", Toast.LENGTH_SHORT).show();


    }
}
